package comgame.scenes;

import com.almasb.fxgl.audio.AudioPlayer;
import com.almasb.fxgl.audio.Sound;
import com.almasb.fxgl.dsl.FXGL;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;

import java.util.List;
import java.util.ArrayList;

/**
 * 战斗结果UI类
 */
public class BattleResultUI extends StackPane {
    private boolean isVictory;
    private List<String> rewards;
    private Runnable onContinue;
    private Runnable onReturn;
    private Runnable onRetry; // 新增再次挑战回调
    private int level; // 添加关卡参数
    private boolean buttonClicked = false; // 防抖标志
    
    public BattleResultUI(boolean isVictory, List<String> rewards, Runnable onContinue, Runnable onReturn) {
        this.isVictory = isVictory;
        this.rewards = rewards != null ? rewards : new ArrayList<>();
        this.onContinue = onContinue;
        this.onReturn = onReturn;
        this.onRetry = null; // 默认值
        this.level = 0; // 默认值
        initUI();
    }
    
    public BattleResultUI(boolean isVictory, List<String> rewards, Runnable onContinue, Runnable onReturn, int level) {
        this.isVictory = isVictory;
        this.rewards = rewards != null ? rewards : new ArrayList<>();
        this.onContinue = onContinue;
        this.onReturn = onReturn;
        this.onRetry = null; // 默认值
        this.level = level;
        initUI();
    }
    
    // 新增构造函数，支持再次挑战回调
    public BattleResultUI(boolean isVictory, List<String> rewards, Runnable onContinue, Runnable onReturn, Runnable onRetry, int level) {
        this.isVictory = isVictory;
        this.rewards = rewards != null ? rewards : new ArrayList<>();
        this.onContinue = onContinue;
        this.onReturn = onReturn;
        this.onRetry = onRetry;
        this.level = level;
        initUI();
    }
    
    private void initUI() {
        // 设置UI样式 - 半透明黑色背景覆盖整个屏幕
        setStyle("-fx-background-color: rgba(0, 0, 0, 0.7);");
        setPrefSize(FXGL.getAppWidth(), FXGL.getAppHeight());
        
        // 创建主容器 - 居中显示，缩小尺寸
        VBox mainContainer = new VBox(20);
        mainContainer.setAlignment(Pos.CENTER);
        mainContainer.setPadding(new Insets(30));
        mainContainer.setPrefSize(550, 450);
        
        // 根据战斗结果设置不同的背景色和样式
        if (isVictory) {
            mainContainer.setStyle("-fx-background-color: linear-gradient(to bottom, #27AE60, #229954); " +
                                "-fx-background-radius: 20; " +
                                "-fx-border-color: #2ECC71; " +
                                "-fx-border-width: 3; " +
                                "-fx-border-radius: 20; " +
                                "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 10, 0, 0, 5);");
        } else {
            mainContainer.setStyle("-fx-background-color: linear-gradient(to bottom, #E74C3C, #C0392B); " +
                                "-fx-background-radius: 20; " +
                                "-fx-border-color: #EC7063; " +
                                "-fx-border-width: 3; " +
                                "-fx-border-radius: 20; " +
                                "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 10, 0, 0, 5);");
        }
        
        // 标题容器
        VBox titleContainer = new VBox(5);
        titleContainer.setAlignment(Pos.CENTER);
        titleContainer.setPadding(new Insets(15, 25, 15, 25));
        titleContainer.setStyle("-fx-background-color: rgba(255, 255, 255, 0.1); -fx-background-radius: 15;");
        
        Label titleLabel = new Label(isVictory ? "🎉 挑战成功！" : "💀 挑战失败");
        titleLabel.setStyle("-fx-font-size: 28px; -fx-font-weight: bold; -fx-text-fill: white;");
        
        Label subtitleLabel = new Label(isVictory ? "你击败了敌人！" : "你被击败了...");
        subtitleLabel.setStyle("-fx-font-size: 16px; -fx-text-fill: #F8F9FA;");
        
        titleContainer.getChildren().addAll(titleLabel, subtitleLabel);
        
        // 结果信息
        VBox resultInfo = createResultInfo();
        
        // 按钮容器
        HBox buttonContainer = createButtons();
        
        mainContainer.getChildren().addAll(titleContainer, resultInfo, buttonContainer);
        
        // 将主容器居中显示
        StackPane.setAlignment(mainContainer, Pos.CENTER);
        getChildren().add(mainContainer);
    }
    
    private VBox createResultInfo() {
        VBox container = new VBox(15);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(20, 25, 20, 25));
        container.setStyle("-fx-background-color: rgba(255, 255, 255, 0.1); -fx-background-radius: 15; " +
                         "-fx-border-color: rgba(255, 255, 255, 0.2); -fx-border-width: 1; -fx-border-radius: 15;");
        
        if (isVictory) {
            // 胜利时显示奖励
            Label rewardTitle = new Label("🏆 获得奖励：");
            rewardTitle.setStyle("-fx-font-size: 20px; -fx-font-weight: bold; -fx-text-fill: #F1C40F;");
            
            container.getChildren().add(rewardTitle);
            
            if (rewards.isEmpty()) {
                Label noReward = new Label("💭 暂无奖励");
                noReward.setStyle("-fx-font-size: 16px; -fx-text-fill: #BDC3C7; -fx-font-style: italic;");
                container.getChildren().add(noReward);
            } else {
                VBox rewardsContainer = new VBox(6);
                rewardsContainer.setAlignment(Pos.CENTER);
                
                for (String reward : rewards) {
                    Label rewardLabel = new Label("✨ " + reward);
                    rewardLabel.setStyle("-fx-font-size: 16px; -fx-text-fill: #F1C40F; -fx-font-weight: 500;");
                    rewardsContainer.getChildren().add(rewardLabel);
                }
                container.getChildren().add(rewardsContainer);
            }
            
            // 添加详细掉落物信息
            VBox dropInfo = createDropInfo();
            if (dropInfo != null) {
                container.getChildren().add(dropInfo);
            }
        } else {
            // 失败时显示提示
            VBox failContainer = new VBox(8);
            failContainer.setAlignment(Pos.CENTER);
            
            Label failMessage = new Label("💀 你被击败了...");
            failMessage.setStyle("-fx-font-size: 18px; -fx-text-fill: white; -fx-font-weight: bold;");
            
            Label tipMessage = new Label("💪 提升装备后再次挑战吧！");
            tipMessage.setStyle("-fx-font-size: 14px; -fx-text-fill: #F1C40F; -fx-font-style: italic;");
            
            failContainer.getChildren().addAll(failMessage, tipMessage);
            container.getChildren().add(failContainer);
        }
        
        return container;
    }
    
    // 创建详细掉落物信息
    private VBox createDropInfo() {
        if (level == 0) return null; // 如果没有关卡信息，不显示
        
        VBox container = new VBox(12);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(15, 0, 0, 0));
        container.setStyle("-fx-background-color: rgba(255, 255, 255, 0.05); -fx-background-radius: 10; " +
                         "-fx-border-color: rgba(255, 255, 255, 0.1); -fx-border-width: 1; -fx-border-radius: 10;");
        
        Label dropTitle = new Label("📋 关卡掉落物详情：");
        dropTitle.setStyle("-fx-font-size: 14px; -fx-font-weight: bold; -fx-text-fill: #3498DB;");
        container.getChildren().add(dropTitle);
        
        VBox dropDetails = new VBox(4);
        dropDetails.setAlignment(Pos.CENTER);
        
        switch (level) {
            case 1:
                dropDetails.getChildren().addAll(
                    createDropLabel("史莱姆凝胶 2-3个 (100%) - 单价2元"),
                    createDropLabel("随机一级作物种子 2个 (10%)")
                );
                break;
            case 2:
                dropDetails.getChildren().addAll(
                    createDropLabel("昆虫肉 1-2个 (100%) - 单价5元"),
                    createDropLabel("霜瓜种子 1-2个 (20%)")
                );
                break;
            case 3:
                dropDetails.getChildren().addAll(
                    createDropLabel("妙蛙种子 (可服役) 50%"),
                    createDropLabel("捕捉失败 (50%)"),
                    createDropLabel("普通级 (30%) - 2秒一步自动"),
                    createDropLabel("优秀级 (15%) - 1秒自动"),
                    createDropLabel("史诗级 (4%) - 0.5秒一步"),
                    createDropLabel("传说级 (1%) - 0.1秒一步")
                );
                break;
            case 4:
                dropDetails.getChildren().addAll(
                    createDropLabel("装备掉落："),
                    createDropLabel("  什么都没有 (30%)"),
                    createDropLabel("  幽灵剑 (15%)"),
                    createDropLabel("  幽灵一套 (55%/4%)"),
                    createDropLabel("种子掉落："),
                    createDropLabel("  霜瓜 3-4个 (50%)")
                );
                break;
            case 5:
                dropDetails.getChildren().addAll(
                    createDropLabel("随机所有一级种子 3-5个 (80%)"),
                    createDropLabel("随机所有二级种子 2-4个 (15%)"),
                    createDropLabel("齐瓜 (5%)")
                );
                break;
            case 6:
                dropDetails.getChildren().addAll(
                    createDropLabel("南瓜王 (50%)："),
                    createDropLabel("  无头骑士剑 (15%)"),
                    createDropLabel("  南瓜种子 100个 (100%)"),
                    createDropLabel("常绿尖叫怪 (50%)："),
                    createDropLabel("  喜庆之翼 (10%)")
                );
                break;
            case 7:
                dropDetails.getChildren().addAll(
                    createDropLabel("空间之主一套 (20%)"),
                    createDropLabel("泰拉棱镜 (20%)")
                );
                break;
            case 8:
                dropDetails.getChildren().addAll(
                    createDropLabel("世纪之花！ 100%幼苗 只能打一次")
                );
                break;
        }
        
        container.getChildren().add(dropDetails);
        return container;
    }
    
    private Label createDropLabel(String text) {
        Label label = new Label(text);
        label.setStyle("-fx-font-size: 12px; -fx-text-fill: #E8F4FD; -fx-font-weight: 400;");
        return label;
    }
    
    private HBox createButtons() {
        HBox container = new HBox(20);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(15, 0, 0, 0));
        
        // 加载音效
        AudioPlayer audioPlayer = FXGL.getAudioPlayer();
        Sound clickSound = FXGL.getAssetLoader().loadSound("drop.wav");
        
        if (isVictory && level != 8) {
            // 胜利时显示下一关、再次挑战和返回按钮（非8关）
            
            // 对于第7关，只显示再次挑战和返回按钮（不显示下一关）
            if (level == 7) {
                // 再次挑战按钮
                Button retryBtn = new Button("🔄 再次挑战");
                retryBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #E67E22, #D35400); " +
                                "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                "-fx-border-color: #D35400; -fx-border-width: 2; -fx-border-radius: 10; " +
                                "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
                
                // 添加悬停效果
                retryBtn.setOnMouseEntered(e -> {
                    retryBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #D35400, #BA4A00); " +
                                    "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                    "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                    "-fx-border-color: #BA4A00; -fx-border-width: 2; -fx-border-radius: 10; " +
                                    "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 8, 0, 0, 3);");
                });
                
                retryBtn.setOnMouseExited(e -> {
                    retryBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #E67E22, #D35400); " +
                                    "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                    "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                    "-fx-border-color: #D35400; -fx-border-width: 2; -fx-border-radius: 10; " +
                                    "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
                });
                
                retryBtn.setOnMouseClicked(e -> {
                    if (buttonClicked) return; // 防抖：如果已经点击过，直接返回
                    buttonClicked = true;
                    audioPlayer.playSound(clickSound);
                    if (onRetry != null) {
                        onRetry.run();
                    }
                });
                
                container.getChildren().add(retryBtn);
            } else {
                // 对于其他关卡（1-6关），显示下一关和再次挑战按钮
                Button continueBtn = new Button("⚔ 下一关");
                continueBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #3498DB, #2980B9); " +
                                  "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                  "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                  "-fx-border-color: #2980B9; -fx-border-width: 2; -fx-border-radius: 10; " +
                                  "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
                
                // 添加悬停效果
                continueBtn.setOnMouseEntered(e -> {
                    continueBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #2980B9, #21618C); " +
                                      "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                      "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                      "-fx-border-color: #21618C; -fx-border-width: 2; -fx-border-radius: 10; " +
                                      "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 8, 0, 0, 3);");
                });
                
                continueBtn.setOnMouseExited(e -> {
                    continueBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #3498DB, #2980B9); " +
                                      "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                      "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                      "-fx-border-color: #2980B9; -fx-border-width: 2; -fx-border-radius: 10; " +
                                      "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
                });
                
                            continueBtn.setOnMouseClicked(e -> {
                if (buttonClicked) return; // 防抖：如果已经点击过，直接返回
                buttonClicked = true;
                audioPlayer.playSound(clickSound);
                if (onContinue != null) {
                    onContinue.run();
                }
            });
                
                container.getChildren().add(continueBtn);
                
                // 再次挑战按钮
                Button retryBtn = new Button("🔄 再次挑战");
                retryBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #E67E22, #D35400); " +
                                "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                "-fx-border-color: #D35400; -fx-border-width: 2; -fx-border-radius: 10; " +
                                "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
                
                // 添加悬停效果
                retryBtn.setOnMouseEntered(e -> {
                    retryBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #D35400, #BA4A00); " +
                                    "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                    "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                    "-fx-border-color: #BA4A00; -fx-border-width: 2; -fx-border-radius: 10; " +
                                    "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 8, 0, 0, 3);");
                });
                
                retryBtn.setOnMouseExited(e -> {
                    retryBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #E67E22, #D35400); " +
                                    "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                                    "-fx-padding: 10 25; -fx-background-radius: 10; " +
                                    "-fx-border-color: #D35400; -fx-border-width: 2; -fx-border-radius: 10; " +
                                    "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
                });
                
                retryBtn.setOnMouseClicked(e -> {
                    if (buttonClicked) return; // 防抖：如果已经点击过，直接返回
                    buttonClicked = true;
                    audioPlayer.playSound(clickSound);
                    if (onRetry != null) {
                        onRetry.run();
                    }
                });
                
                container.getChildren().add(retryBtn);
            }
        }
        // 返回按钮
        Button returnBtn = new Button("🔙 返回");
        returnBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #95A5A6, #7F8C8D); " +
                         "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                         "-fx-padding: 10 30; -fx-background-radius: 10; " +
                         "-fx-border-color: #7F8C8D; -fx-border-width: 2; -fx-border-radius: 10; " +
                         "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
        // 添加悬停效果
        returnBtn.setOnMouseEntered(e -> {
            returnBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #7F8C8D, #6C7B7D); " +
                             "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                             "-fx-padding: 10 30; -fx-background-radius: 10; " +
                             "-fx-border-color: #6C7B7D; -fx-border-width: 2; -fx-border-radius: 10; " +
                             "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 8, 0, 0, 3);");
        });
        returnBtn.setOnMouseExited(e -> {
            returnBtn.setStyle("-fx-background-color: linear-gradient(to bottom, #95A5A6, #7F8C8D); " +
                             "-fx-text-fill: white; -fx-font-size: 16px; -fx-font-weight: bold; " +
                             "-fx-padding: 10 30; -fx-background-radius: 10; " +
                             "-fx-border-color: #7F8C8D; -fx-border-width: 2; -fx-border-radius: 10; " +
                             "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 5, 0, 0, 2);");
        });
        returnBtn.setOnMouseClicked(e -> {
            if (buttonClicked) return; // 防抖：如果已经点击过，直接返回
            buttonClicked = true;
            audioPlayer.playSound(clickSound);
            if (onReturn != null) {
                onReturn.run();
            }
        });
        container.getChildren().add(returnBtn);
        return container;
    }
} 